#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5;
typedef long long LL;
LL cal(string A,LL k){
    LL sum=0;
    for(LL i=0;i<A.size();i++){
        sum=sum*k+(A[i]-'0');
   ///     debug(sum);
    }
    return sum;
}
bool check(LL num,LL mid,string B){
    LL sum=cal(B,mid);
   /// debug(sum);
    if(sum>=num) return true;
    else return false;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL t;cin>>t;
  while(t--){
    string A,B;cin>>A>>B;
  ///  cout<<A<<endl;
  ///  cout<<B<<endl;
    for(LL i=10;i<=15000;i++){
        LL x=cal(A,i);

   ///     debug(x);
        LL l=9;LL r=15001;
        while(l<r){
            LL mid=(l+r)>>1;
            if(check(x,mid,B)) r=mid;
            else l=mid+1;
        }
///        debug(l);
        if(l<10||l>15000) continue;
        LL num=cal(B,l);
        if(num==x) cout<<i<<" "<<l<<endl;
    }
  }
return 0;
}
